package com.leo.javase.thread.itcast;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest {

	public static void main(String[] args) {
		
		new ThreadPoolTest().testExecutorsSchedue();
	}
	
	public void testExecutorsSchedue(){
		Executors.newScheduledThreadPool(3).scheduleAtFixedRate(
						new Runnable() {
							@Override
							public void run() {
								System.out.println("bombing!");
							}
						}, 6, 2, TimeUnit.SECONDS);
	}
	
	
	/**
	 * 线程池
	 */
	public void testExecutors(){
		//ExecutorService pool = Executors.newFixedThreadPool(3);
		//ExecutorService pool = Executors.newCachedThreadPool();
		ExecutorService pool = Executors.newSingleThreadExecutor();
		
		for(int i=0; i<10; i++){
			final int task = i;
			pool.execute(new Runnable() {
				@Override
				public void run() {
					for(int i=0; i<8; i++){
						System.out.println(Thread.currentThread().getName()+" is looping of "+(i+1)+"  for task of "+(task+1));
					}
				}
			});
		}
		
		System.out.println("10 of tasks have comiited !");
		pool.shutdown();
	}
}
